import requests

from bs4 import BeautifulSoup

url = 'https://movie.douban.com/top250'
# 设置浏览器代理，它是一个字典
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/74.0.3724.8 Safari/537.36 '
}
# 请求源代码向服务器发出请求
response = requests.get(url=url, headers=headers)
html_content = response.text
# print(html_content)

soup = BeautifulSoup(html_content, 'html.parser')

# 通过标签、类名或其他选择器提取电影信息
movies = soup.find_all('div', class_='item')

for movie in movies:
    title = movie.find('span', class_='title').text
    rating = movie.find('span', class_='rating_num').text
    print(f'Title: {title}, Rating: {rating}')
